package com.practice.J2E.DesignMode.strategy;

import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 动物工厂
 * @author kayn
 * @create 2023-09-08 下午 04:22
 */
public class AnimalFactory {

    //动物池
    public static Map<String,Animal> animalPool;

    /**
     * 初始化动物池
     */
    static {

        System.out.println("***********************animalPool initialization start***********************");

        animalPool = new HashMap<>();

        animalPool.put("cat",new Cat());

        System.out.println("cat init successfully");

        animalPool.put("dog",new Dog());

        System.out.println("dog init successfully");

        System.out.println("***********************animalPool initialization finish***********************");

    }

    /**
     * 获取一只小动物
     * @param animalName
     * @return
     */
    public static Animal takeAnimalFromPool(String animalName) {

        for (String key : animalPool.keySet()) {

            if (StrUtil.equals(animalName,key)) {

                return animalPool.get(animalName);

            }

        }

        System.out.println("未定义名为" + animalName + "的小动物！");

        return () -> System.out.println(animalName + " speak");
    }
}
